; #include C:\Program Files\AutoHotkey\ahk-lib\lib\gdip\GdipStarter.ahk
#Include <ahk-lib\lib\gdip\GdipStarter>
; #include C:\Program Files\AutoHotkey\ahk-lib\util\Cursor.ahk
#Include <ahk-lib\util\Cursor>

global staticG := true
global offsetX := offsetY := 12, width := height := 160, _h := 16
global font := "consolas", fc := '#ffdbffd5'.substring(2)

;设置Gdip字体
Gdip_FontFamilyCreate(Font)
;开始获取颜色
startGetColor(HotkeyName) {
    WinActivate('技能取色')
    global pBitmap, hBitmap, staticHdc, staticObm, pBrush, pPenLine, pPenbkBlack, pPenbkWhite
    pBrush := Gdip_BrushCreateSolid(0x8f000000)
    pPenLine := Gdip_CreatePen(0x60ffffff, 1)
    pPenbkBlack := Gdip_CreatePen(0xff000000, 1)
    pPenbkWhite := Gdip_CreatePen(0xffffffff, 1)

    pBitmap := Gdip_BitmapFromScreen()
        , hBitmap := Gdip_CreateHBITMAPFromBitmap(pBitmap) ;将 GDI+ 的 Bitmap 对象转换为 HBITMAP 句柄
        , staticHdc := CreateCompatibleDC() ;
        , staticObm := SelectObject(staticHdc, hBitmap)
    global gui_ := Gui('-Caption +ToolWindow +E0x00080000')
    pic := gui_.AddPic()
    gui_.Show('NA')
    SetTimer(getColor, 50)
}
;设置取色相关热键
globalHotKeySet() {
    global flag, _h
    HotIfWinExist('技能取色')
    Hotkey('F2', startGetColor)
    Hotkey('WheelUp', (*) => (_h -= _h <= 8 ? 0 : 2, flag := false))
    Hotkey('WheelDown', (*) => (_h += _h >= height ? 0 : 2, flag := false))
    Hotkey('Enter', exitColor)
    Hotkey('NumpadEnter', exitColor)
    Hotkey('Esc', cancelGetColor)
    Hotkey('Left', (*) => MouseMove(-1, 0, , 'R'))
    Hotkey('Right', (*) => MouseMove(1, 0, , 'R'))
    Hotkey('Up', (*) => MouseMove(0, -1, , 'R'))
    Hotkey('Down', (*) => MouseMove(0, 1, , 'R'))
}

cancelGetColor(*) {
    exitColor()
    mainGuiShoworHide()
}

exitColor(*) {
    Cursor.SetIcon(Cursor.Icon.arrow)
    SetTimer(getColor, 0)
    try gui_.Destroy()
}

getColor() {
    global pBitmap, hBitmap, staticHdc, staticObm
    Cursor.SetIcon(Cursor.Icon.cross)
    static o_mX := 0, o_mY := 0
    MouseGetPos(&n_mX, &n_mY)
    if (n_mX = o_mX && n_mY = o_mY && flag) {
        return
    }
    global flag := true
    o_mX := n_mX, o_mY := n_mY
    hbm := CreateDIBSection(A_ScreenWidth, A_ScreenHeight)
    hdc := CreateCompatibleDC(), obm := SelectObject(hdc, hbm)
    G := Gdip_GraphicsFromHDC(hdc), Gdip_SetSmoothingMode(G, 4)
    if staticG  ; static background
        BitBlt(hdc, 0, 0, A_ScreenWidth, A_ScreenHeight, staticHdc, 0, 0)
    ;绘制放大区域的边框以及十字线
    _DrawEnlargementfiFrame(n_mX, n_mY)

    _DrawPic(n_mX, n_mY)
    UpdateLayeredWindow(gui_.Hwnd, hdc, 0, 0, A_ScreenWidth, A_ScreenHeight)
    SelectObject(hdc, obm) DeleteObject(hbm), DeleteDC(hdc), Gdip_DeleteGraphics(G)

    _DrawPic(x, y) {
        x := x + offsetX + width + 5 > A_ScreenWidth ? x - 6 : x + offsetX + width - 1
        y := y + offsetY + height + 25 > A_ScreenHeight ? y - height + 26 : y + offsetY + 51
    }

    _DrawEnlargementfiFrame(mx, my) {
        global _h
        ;ColorGui的colorX控件的值为当前鼠标位置的X坐标
        ;ColorGui的colorY控件的值为当前鼠标位置的Y坐标
        color.colorX.Text := mx, color.colorY.Text := my
        _offsetX := mx + offsetX + width + 5 > A_ScreenWidth ? -width - 5 : offsetX ;鼠标偏移
        _offsetY := my + offsetY + height + 25 > A_ScreenHeight ? -height - 25 : offsetY ;鼠标偏移
        _hbm := CreateDIBSection(A_ScreenWidth, A_ScreenHeight)
        _hdc := CreateCompatibleDC()
        _obm := SelectObject(_hdc, _hbm), _G := Gdip_GraphicsFromHDC(_hdc)
        BitBlt(_hdc, 0, 0, A_ScreenWidth, A_ScreenHeight, staticHdc, 0, 0)
        _w := width * _h // height, _pw := width // _w, _ph := height // _h
        Gdip_DrawLine(_G, pPenLine, mx, my - 1, mx, my - _h // 2) ; vertical
        Gdip_DrawLine(_G, pPenLine, mx, my + 1, mx, my + _h // 2)
        Gdip_DrawLine(_G, pPenLine, mx - 1, my, mx - _w // 2, my)
        Gdip_DrawLine(_G, pPenLine, mx + 1, my, mx + _w // 2, my)
        _x := mx + _offsetX, _y := my + _offsetY, cx := _x + (width + 4) // 2, cy := _y + (height + 4) // 2
        StretchBlt(hdc, _x + 2, _y + 2, width, height, _hdc, mx - _w // 2, my - _h // 2, (_w & 1 ? _w : _w + 1), _h
        )
        ;边框
        Gdip_DrawRoundedRectangle(G, pPenbkBlack, _x, _y, width + 2, height + 2, 0) ; border
        Gdip_DrawRoundedRectangle(G, pPenbkWhite, _x + 1, _y + 1, width, height, 0)
        Gdip_DrawRoundedRectangle(G, pPenbkBlack, cx - _pw // 2 - 2, cy - 2, _pw + 3, _ph + 3, 0)
        Gdip_DrawRoundedRectangle(G, pPenbkWhite, cx - _pw // 2 - 1, cy - 1, _pw + 1, _ph + 1, 0)
        ;十字线
        _DrawTip()

        SelectObject(_hdc, _obm), DeleteObject(_hbm), DeleteDC(_hdc), Gdip_DeleteGraphics(_G)

        _DrawTip() {
            local x := _x + 1, y := _y + height + 3
            Gdip_FillRoundedRectangle(G, pBrush, _x, y - 1, width + 2, 22, 0) ; background
            _c := '0xff' (hexC := PixelGetColor(Cursor.x, Cursor.y, 'slow').substring(3))
            ;ColorGui的colorValue控件的值为当前鼠标位置的颜色值
            hex := PixelGetColor(Cursor.x, Cursor.y, 'fast')
            color.colorValue.Text := hex
            Gdip_FillRectangle(G, _b := Gdip_BrushCreateSolid(_c), x, y, 20, 20) ; color box
            Gdip_DrawRoundedRectangle(G, pPenbkWhite, x, y, 20, 20, 0)
            options := Format('x{} y{} c{} Center s15', x + 15, y + 2, fc)
            Gdip_TextToGraphics(G, hex, options, font, width - 20, 30)
            Gdip_DeleteBrush(_b)
        }
    }
}
Clean() {
    ; HotKeysOff('LButton Up', 'RButton Up', 'Esc')
    try {
        Gdip_DeletePen(pPenLine), Gdip_DeletePen(pPenbkWhite)
        Gdip_DeletePen(pPenbkBlack), Gdip_DeleteBrush(pBrush)
        SelectObject(staticHdc, staticObm), DeleteDC(staticHdc), DeleteObject(hBitmap)
        DeleteObject(pBitmap)
    }
}